import { useClipboard } from '@vueuse/core'
import { ElMessage } from 'element-plus'
export function useCopy() {
    const copy = async (text1: string) => {
        let text = filter(text1)
        const { copy } = useClipboard({ source: text })
        try {
            if (navigator.clipboard) {
                setTimeout(async () => {
                    await copy(text)
                }, 0)
            } else {
                const textarea = document.createElement('textarea')
                textarea.value = text
                document.body.appendChild(textarea)
                textarea.select()
                document.execCommand('copy')
                document.body.removeChild(textarea)
            }
            ElMessage.success({ message: '复制成功' })
        } catch (error) {
            console.log(error)
            ElMessage.error({ message: '复制失败' })
        }
    }


    //过滤html标签
    function filter(html) {
        let str = html.replace(/<style[\s\S]*?<\/style>/gi, '')
        return str
            .replace(/<(?:.|\n)*?>/gm, '')
            .replace(/##/g, '')
            .replace(/(&rdquo;)/g, '"')
            .replace(/&ldquo;/g, '"')
            .replace(/&mdash;/g, '-')
            .replace(/&nbsp;/g, '')
            .replace(/&gt;/g, '>')
            .replace(/&lt;/g, '<')
            .replace(/```html/g, '')
            .replace(/```/g, '')
            .replace(/<[\w\s"':=\/]*/, '')
            .replace(/\|/g, '')
            .replace(/-/g, '')
            .replace(/#/g, '')
            .replace(/\*+/g, '')
    }
    return {
        copy
    }
}
